import Cesium from '../Ces/Cesium'
/**
 * @private
 */
function Sampler(options) {
  options = options ?? Cesium.Frozen.EMPTY_OBJECT

  const wrapS = options.wrapS ?? Cesium.TextureWrap.CLAMP_TO_EDGE
  const wrapT = options.wrapT ?? Cesium.TextureWrap.CLAMP_TO_EDGE
  const wrapR = options.wrapR ?? Cesium.TextureWrap.CLAMP_TO_EDGE
  const minificationFilter =
    options.minificationFilter ??
    Cesium.TextureMinificationFilter.LINEAR

  const magnificationFilter =
    options.magnificationFilter ??
    Cesium.TextureMagnificationFilter.LINEAR

  const maximumAnisotropy = Cesium.defined(options.maximumAnisotropy)
    ? options.maximumAnisotropy
    : 1.0;

  // >>includeStart('debug', pragmas.debug);
  if (!Cesium.TextureWrap.validate(wrapS)) {
    throw new Cesium.DeveloperError('Invalid sampler.wrapS.');
  }

  if (!Cesium.TextureWrap.validate(wrapT)) {
    throw new Cesium.DeveloperError('Invalid sampler.wrapT.');
  }

  if (!Cesium.TextureMinificationFilter.validate(minificationFilter)) {
    throw new Cesium.DeveloperError('Invalid sampler.minificationFilter.');
  }

  if (!Cesium.TextureMagnificationFilter.validate(magnificationFilter)) {
    throw new Cesium.DeveloperError('Invalid sampler.magnificationFilter.');
  }

  Cesium.Check.typeOf.number.greaterThanOrEquals(
    'maximumAnisotropy',
    maximumAnisotropy,
    1.0,
  );
  // >>includeEnd('debug');

  this._wrapS = wrapS;
  this._wrapT = wrapT;
  this._wrapR = wrapR;
  this._minificationFilter = minificationFilter;
  this._magnificationFilter = magnificationFilter;
  this._maximumAnisotropy = maximumAnisotropy;
}

Object.defineProperties(Sampler.prototype, {
  wrapS: {
    get() {
      return this._wrapS;
    },
  },
  wrapT: {
    get() {
      return this._wrapT;
    },
  },
  wrapR: {
    get() {
      return this._wrapR;
    },
  },
  minificationFilter: {
    get() {
      return this._minificationFilter;
    },
  },
  magnificationFilter: {
    get() {
      return this._magnificationFilter;
    },
  },
  maximumAnisotropy: {
    get() {
      return this._maximumAnisotropy;
    },
  },
});

Sampler.equals = function (left, right) {
  return (
    left === right
    || (Cesium.defined(left)
      && Cesium.defined(right)
      && left._wrapS === right._wrapS
      && left._wrapT === right._wrapT
      && left._minificationFilter === right._minificationFilter
      && left._magnificationFilter === right._magnificationFilter
      && left._maximumAnisotropy === right._maximumAnisotropy)
  );
};

Sampler.NEAREST = Object.freeze(
  new Sampler({
    wrapS: Cesium.TextureWrap.CLAMP_TO_EDGE,
    wrapT: Cesium.TextureWrap.CLAMP_TO_EDGE,
    wrapR: Cesium.TextureWrap.CLAMP_TO_EDGE,
    minificationFilter: Cesium.TextureMinificationFilter.NEAREST,
    magnificationFilter: Cesium.TextureMagnificationFilter.NEAREST,
  }),
);


Sampler.LINEAR = Object.freeze(
  new Sampler({
    wrapS: Cesium.TextureWrap.CLAMP_TO_EDGE,
    wrapT: Cesium.TextureWrap.CLAMP_TO_EDGE,
    wrapR: Cesium.TextureWrap.CLAMP_TO_EDGE,
    minificationFilter: Cesium.TextureMinificationFilter.LINEAR,
    magnificationFilter: Cesium.TextureMagnificationFilter.LINEAR,
  }),
);

export default Sampler;
